﻿using Avalonia.Controls;
using LightCAD.Core;
using LightCAD.Core.Elements;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.Runtime.Editor
{
    public static class ElementDrawer
    {
        public static SKPathEffect SelectedEffect =  SKPathEffect.CreateDash(new float[] { 3, 3 }, 10);
        public static SKPaint selectedPen = new SKPaint { Color = SKColors.White, IsStroke = true, PathEffect = ElementDrawer.SelectedEffect };
        public static SKPaint defaultPen = new SKPaint { Color = SKColors.Red, IsStroke = true };
        public static SKPaint draggingPen = new SKPaint { Color = SKColors.Gray, IsStroke = true };

        public static SKPaint GetDrawPen(Element element,bool isDragCopy)
        {
            if (isDragCopy) return ElementDrawer.defaultPen;

            if (element.IsDragging)
            {
                return ElementDrawer.draggingPen;
            }
            else if(element.IsSelected)
            {
                return ElementDrawer.selectedPen;
            }
            else
            {
                return ElementDrawer.defaultPen;
            }
        }
        public static Func<bool> GetViewNeedChange { get; set; }
        public static Func<Tuple<SKRect, Box2d>> GetVPortScope { get; set; }
        public static Func<SKMatrix> WorldToScrMatrix { get; set; }
        public static Func<SKPoint, Vector2d> PointScrToWorld { get; set; }
        public static Func<Vector2d,SKPoint> PointWorldToScr { get; set; }
        public static Func<float, double> LengthScrToWorld { get; set; }
        public static Func<double, float> LengthWorldToScr { get; set; }
        /// <summary>
        /// 绘制元素，offset为0时用于正常绘制，offset不为零时用于拖拽/黏贴绘制
        /// </summary>
        /// <param name="element"></param>
        /// <param name="canvas"></param>
        /// <param name="offset"></param>
        public static void Draw(this Element element, SKCanvas canvas,Vector2d? offset=null)
        {
            var ofs = (offset == null) ? Vector2d.Zero : offset.Value;
            switch (element.Type)
            {
                case ElementType.Line:
                    (element as Line).DrawTo(canvas, ofs); break;
                case ElementType.Circle:
                    (element as Circle).DrawTo(canvas, ofs); break;
            }
        }
        public static ControlGrip[] GetControlGrips(this Element element)
        {
            ControlGrip[] grips=null;
            switch (element.Type)
            {
                case ElementType.Line:
                    grips = (element as Line).GetControlGrips(); break;
                case ElementType.Circle:
                    grips = (element as Circle).GetControlGrips(); break;
            }
            return grips;
        }

        public static Action<SKCanvas> SetDragGrip(this Element element,string gripName,Vector2d position,bool isEnd)
        {
            Action<SKCanvas> drawAction = null;
            switch (element.Type)
            {
                case ElementType.Line:
                    drawAction = (element as Line)._SetDragGrip(gripName,position, isEnd); break;
                case ElementType.Circle:
                    drawAction = (element as Circle)._SetDragGrip(gripName, position, isEnd); break;
            }
            return drawAction;
        }
    }
    
}
